package com.doudou.dubbo.demo;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.doudou.dubbo.demo.api.GreetingService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

/**
 * @author zhaozw
 * @version 1.0
 * @date 2025-07-10 20:29
 **/
public class ApiConsumer {

    public static void main(String[] args) {
        // 创建服务引用对象实例
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();
        
        // 设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        
        // 设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        
        // 设置服务接口和超时时间
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setTimeout(5000);
        
        // 设置自定义负载均衡策略与集群容错策略
//        referenceConfig.setLoadbalance("myLoadBalance");
//        referenceConfig.setCluster("myBroadcast");

        // 设置服务分组与版本
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dubbo");

        // 引用服务
        GreetingService greetingService = referenceConfig.get();

        // 设置隐藏参数
        RpcContext.getContext().setAttachment("company", "juzi");

        // 调用服务
        System.out.println(greetingService.sayHello("world"));
    }
}
